class Api::V2::UsersController < Api::V1::UsersController
  include ApplicationHelper

  def show
    # puts "获取到结果",  Rails.application.config_for(:me)[:show_name]
    # puts 123, Rails.configuration.guess
    # puts "获取guess文件参数：" + Rails.configuration.guess[:icon_cdn]
    #

    # my_openssl

    tmp = rsa_public_encrypt
    rsa_private_decrypt(tmp)

    # res = {:name => 'reggie', :pwd => '123abc', :a_china => 'china'}
    # puts res.to_json
    # puts res.sort.to_h.to_json
    #
    # # puts ApplicationHelper::codes
    # # puts format_time(Time.now)
    #
    # @user = User.find(params[:id])
    # api_success(data: @user)
  end

  # OpenSSL 提供加密和解密的对称算法。可用的算法取决于安装的特定版本的 OpenSSL。
  def my_openssl
    # 支持的算法列表可以通过这种方式查看
    # puts OpenSSL::Cipher.ciphers
    # 实例化方法
    # cipher = OpenSSL::Cipher.new('<name>-<key length>-<mode>')

    # 加密使用 cipher.encrypt
    # 解密使用 cipher.decrypt
    # 他们都是在实例上调用
    # 初始化密钥，可以使用 cipher.encrypt, key = cipher.random_key
    # 初始化IV
    # iv = cipher.random_iv
    #
    #
    test_data = "hi reggie, are you ready?"
    cipher = OpenSSL::Cipher.new('AES-256-CBC')
    cipher.encrypt
    key = cipher.random_key
    iv = cipher.random_iv

    # puts "the key is:" + Base64.encode64(key)
    # puts "the iv is:" + iv.to_s
    # 加密
    encrypted = cipher.update(test_data) + cipher.final
    # puts "the sec is:" + test_data
    #
    # 开始解密
    decipher = OpenSSL::Cipher.new('AES-256-CBC')
    decipher.decrypt
    decipher.key = key
    decipher.iv = iv
    plain = decipher.update(encrypted) + decipher.final

    puts "des is:" + plain
  end

  # 使用公钥来加密
  def rsa_public_encrypt
    # test_data = {"user_id" => 1, "name" => "reggie", "user_key" => "xx233"}.to_json
    test_data = {:user_id => 1, :name => "reggie", :user_key => "xx233"}.to_json
    puts "加密数据：" + test_data
    public_key_path = Rails.root.join("config", "rsa", "rsa_public_key.pem");
    public_key_content = File.read(public_key_path);
    rsa_pub = OpenSSL::PKey::RSA.new(public_key_content)
    buff = rsa_pub.public_encrypt(test_data)
    base64_buff = Base64.encode64(buff)
    puts "Base64后的密文:", base64_buff
    return base64_buff
  end

  # 使用私钥来解密
  def rsa_private_decrypt(de_data = nil)
    if !de_data
      de_data = "E2CMUrvqXoOdLZG7uauufQTn7smnE31aWzQgAD14rxtboCNoRgXOnVzMHQca912Uvpk1OMrhtJPmtY4AqLsEgzcelzA05X33w7O2bUVRMfAsjjV1nTylg8ZJfchIfLdhQIqL7bQHxpQaIT6+KXlbowXqz21KoQLpCWI6PAfcoAkfv5f+5Q7kANMr+b7lVEwJf3IbbI7uHTJk+GtxZClpkvWxfNNhRM483UqdD/gZK07zybIwjoYO1fc4WB2gitySMZ+p/anuvHvcewcQG2z7yYMfExXoUeafpKhBANibZYB4CMLv2YDtvcz1aK/odL60Fp9rmS4MQ64+bxiBltQ5jg=="
    end

    private_key_path = Rails.root.join("config", "rsa", "rsa_private_key.pem")
    rsa_pri = OpenSSL::PKey::RSA.new(File.read(private_key_path))

    # 密文先进行base64解码
    #  {:user_id=>1, :name=>"reggie", :user_key=>"xx233"}
    base64_de_data = Base64.decode64(de_data)
    buff = rsa_pri.private_decrypt(base64_de_data)
    # puts "测试结果：", buff[:user_id]
    # a = MultiJson.load('{"abc":"def"}')
    # puts "a is:", a["abc"]

    puts "the buff:" + buff
    tmp = MultiJson.load(buff)
    puts "tmp解析为：", tmp['name']
    # eval(buff)
    # puts "解密出的结果是：", buff.class
  end
end
